package org.texteasy.views;

import org.texteasy.R;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.mobclix.android.sdk.MobclixAdView;
import com.mobclix.android.sdk.MobclixAdViewListener;
import com.mobclix.android.sdk.MobclixMMABannerXLAdView;

public class AdBannersView extends LinearLayout implements MobclixAdViewListener{

	public static final int AD_BANNER_CLICKED = R.id.TextEasyBanner;
	
	public AdBannersView(Context arg0, AttributeSet attrs) {
		super(arg0, attrs);
	}
	
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		
		((Activity)getContext()).getLayoutInflater().inflate(R.layout.ad_banners, this);
		
		MobclixMMABannerXLAdView bannerAd = (MobclixMMABannerXLAdView)findViewById(R.id.banner_adview);
		bannerAd.addMobclixAdViewListener(this);
		
		//findViewById(R.id.TextEasyBanner).setOnClickListener(this);
	}
	
	public void setOnClickListener(View.OnClickListener listener){
		findViewById(AD_BANNER_CLICKED).setOnClickListener(listener);
	}
	
	public String keywords() {
		return "bulk text, mass text, select contacts, select groups";
	}
	
	public void onCustomAdTouchThrough(MobclixAdView arg0, String arg1) {
	}
	
	public void onFailedLoad(MobclixAdView v, int arg1) {
		Log.i(getClass().getSimpleName(), "Error code: " + arg1);
		v.pause();
		View view = findViewById(R.id.progress_bar);
		view.setVisibility(View.GONE);
		view = findViewById(R.id.banner_adview);
		view.setVisibility(View.GONE);
		//if the add is disabled then hide everything, else show the 
		//emergency banner
		if(arg1 != MobclixAdViewListener.ADSIZE_DISABLED){
			view = findViewById(R.id.TextEasyBanner);
			view.setVisibility(View.VISIBLE);
		}
	}
	
	public void onSuccessfulLoad(MobclixAdView arg0) {
		View view = findViewById(R.id.progress_bar);
		view.setVisibility(View.GONE);
		//textEasyBanner should already have visibility gone, but
		//just in case
		view = findViewById(R.id.TextEasyBanner);
		view.setVisibility(View.GONE);
		view = findViewById(R.id.banner_adview);
		view.setVisibility(View.VISIBLE);
	}
	
	public String query() {
		return null;
	}
	
}
